<script setup>
import {ref} from 'vue';
import {useRouter} from 'vue-router';
import {ElMessage} from 'element-plus';
import {User, Lock, View, Hide} from '@element-plus/icons-vue';
import {getCaptchaApi} from "../service/login.js";
// 使用userStore
import {useUserStore} from '../store/user';
const userStore = useUserStore();


const router = useRouter();
const ruleForm = ref({
  username: '',
  password: '',
  uuid: '',
  verify: '',
});

const rules = ref({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
  verify: [
    {required: true, message: '请输入验证码', trigger: 'blur'},
  ],
});

const ruleFormRef = ref(null);
const captchaSrc = ref(''); // 假设验证码的 API 地址
const showPassword = ref(false);
const handleLogin = async () => {
  const form = ruleForm.value;

  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      try { 
        await userStore.login(form);
        if (userStore.token) {
          router.push('/');
        }
      } catch (error) {
        ElMessage.error(error.message);
      }
    } else {
      ElMessage.error('请填写完整的表单');
    }
  });
};

const refreshCaptcha = () => {
  getCaptchaApi().then(res => {
    console.log(res);
    ruleForm.value.uuid = res.data.uuid;
    captchaSrc.value = res.data.captcha;
  });
};

const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value;
};

onMounted(() => {
  refreshCaptcha();
});
</script>

<template>
  <el-main class="login-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-position="top" class="login-form">
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" placeholder="请输入用户名" size="large">
          <template #prefix>
            <el-icon size="14px">
              <User/>
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" :type="showPassword?'text':'password'" placeholder="请输入密码"
                  size="large">
          <template #prefix>
            <el-icon size="14px">
              <Lock/>
            </el-icon>
          </template>
          <template #suffix>
            <el-icon @click="togglePasswordVisibility" class="icon-clicked" size="14px">
              <component :is="showPassword ? View : Hide"/>
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="verify">
        <el-row>
          <el-col :span="12">
            <el-input v-model="ruleForm.verify" placeholder="请输入验证码" size="large"/>
          </el-col>
          <el-col :span="12">
            <img v-if="captchaSrc" class="captcha" :src="captchaSrc" @click="refreshCaptcha" alt="验证码"
                 style="cursor: pointer;"/>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin" class="full-width-button" size="large">登录</el-button>
      </el-form-item>
    </el-form>
  </el-main>
</template>

<style scoped lang="less">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  padding-top: 10vh; /* 页面居中偏下一点 */
  background: url("@/assets/img/login.png") no-repeat center center fixed;

  .login-form {
    width: 300px; /* 表单大小 */

    .el-form-item {
      margin-bottom: 20px;
      cursor: pointer;
    }

    .el-row {
      display: flex;
      align-items: center;
    }

    .full-width-button {
      width: 100%;
    }

    .icon-clicked {
      cursor: pointer;
    }

    .captcha {
      width: 100%;
      vertical-align: middle;
      padding-left: 6px;
      box-sizing: border-box;
    }
  }
}


</style>
